package com.penghaisoft.microwms.base.web.data.controller;

import com.penghaisoft.microwms.base.web.data.service.IMdAreaService;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.MdArea;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/data/area")
@Slf4j
public class MdAreaController extends BaseController {

	@Autowired
	private IMdAreaService mdAreaService;

	/**
	 * 新增记录
	 * @param mdArea
	 * @return
	 */
	@PostMapping("c")
	public TransResult create(MdArea mdArea) {
		mdArea.preInsert();
		mdArea.setFactoryCode(getCurrentFactoryCode());
		mdArea.setCreateBy(getLoginName());
		Resp resp =  mdAreaService.create(mdArea);
		return fromResp(resp);
	}

	/**
	 * 删除记录（逻辑删除active_flag=0)
	 * @param mdArea
	 * @return
	 */
	@PostMapping("d")
	public TransResult delete(MdArea mdArea) {
		mdArea.preUpdate();
		mdArea.setLastModifiedBy(getLoginName());
		Resp resp =  mdAreaService.delete(mdArea);
		return fromResp(resp);
	}

	/**
	 * 查询列表
	 * @param
	 * @return
	 */
	@PostMapping("list")
	public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, MdArea mdArea) {
		mdArea.setFactoryCode(getCurrentFactoryCode());
		Pager<MdArea> result = mdAreaService.findListByCondition(page,rows,mdArea);
		return success(result);
	}

	/**
	 * 查询单条
	 * @param id
	 * @return
	 */
	@PostMapping("r/{id}")
	public TransResult queryById(@PathVariable String id) {
		MdArea  mdArea = mdAreaService.findById(id);
		return success(mdArea);
	}

	/**
	 * 修改记录
	 * @param mdArea
	 * @return
	 */
	@PostMapping("u")
	public TransResult update(MdArea mdArea) {
		mdArea.preUpdate();
		mdArea.setLastModifiedBy(getLoginName());
		mdArea.setFactoryCode(getCurrentFactoryCode());
		Resp resp =  mdAreaService.update(mdArea);
		return fromResp(resp);
	}
	
	/**
	 * @Title queryAreasUnderWareHouse
	 * @Description 查询仓库下的区域
	 * @author jingzh
	 * @date 2017年5月24日 下午4:14:16
	 * @param whCode 仓库编码
	 * @return TransResult
	 */
	@PostMapping("r/{whCode}/area")
	public TransResult queryAreasUnderWareHouse(
			@PathVariable(required = true) String whCode) {
		MdArea condition = new MdArea();
		if ("all".equals(whCode)) {
			condition.setWhCode(null);
		} else {
			condition.setWhCode(whCode);
		}
		List<MdArea> mdAreaList = mdAreaService
				.queryAreasUnderWareHouse(condition);
		return success(mdAreaList);
	}

}
